package com.example.demo.controller;


import java.util.List;


import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


import com.example.demo.entity.User;
import com.example.demo.service.User_Service;
import com.example.demo.utils.Md5Utils;
import com.example.demo.utils.ResultInfo;

@RequestMapping("User")
@RestController
public class User_Controller extends Basic_Controller<User>{
	@Autowired
	User_Service service;
	
	@RequestMapping("login")
	@ResponseBody
	public  ResultInfo login(User u,HttpSession session) {
	
		String pass="";
		
		
		if(u.getPassword().length()>0) {
			
			pass=Md5Utils.getMD5Str(u.getPassword());
			
		}
		List<User> list=service.login(u);
		if(list.size()==1) {
			session.setMaxInactiveInterval(5000);
			session.setAttribute("user", list.get(0));
			return new ResultInfo(1);
			
		}else{
			if(list.size()>1) return new ResultInfo(-1,"用户名非法");
			else  return new ResultInfo(-2,"用户名或密码错误");
				
			
		}		
		
	}

	@RequestMapping("outlogin")
	public  ResultInfo outlogin(HttpSession session) {
		session.removeAttribute("user");
		
		return new ResultInfo(1);
	}
}
